import strutils

proc largestProduct*(s: string, n: int): int = 
  if n < 0 or s.len < n or not s.allCharsInSet(Digits):
    raise newException(ValueError, "rejects empty string and nonzero span")
  
  var 
    zeros = 0
    pro = 1

  # calculate the slide window [0..<n]
  for i in 0..<n:
    if s[i] != '0':
      pro = pro * (s[i].ord - '0'.ord)
    else:
      zeros.inc
  result = if zeros == 0: pro else: 0

  for i in n..<s.len:
    # speed up
    if s[i - n] == s[i]: continue

    # pop head
    if s[i - n] != '0':
      pro = pro div (s[i - n].ord - '0'.ord)
    else:
      zeros.dec

    # push tail
    if s[i] != '0':
      pro = pro * (s[i].ord - '0'.ord)
    else:
      zeros.inc

    # recalculate the result
    if zeros == 0 and pro > result:
      result = pro